package com.scy.java.loop;

import java.util.Scanner;

//使用Dowhile循环猜数字小游戏
public class DoWhileDemo2 {
    public static void main(String[] args) {
//        生成随机数作为比较的目标值
        int num = (int)(Math.random()*100+1);
        //生成一个变量记录猜的次数
        int count  = 1 ;
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入一个整数1-100:");
        int guess = scanner.nextInt();
        do{
            if (guess > num){
                System.out.println("猜大了");
            }else if(guess < num){
                System.out.println("猜小了");
            }
            System.out.println("请输入一个整数1-100:");
            guess = scanner.nextInt();
            count++;
        }while (guess != num );
        System.out.println("猜对了");
        System.out.println("共猜了"+count+"次");
    }
}
